﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using V5_BLL;
using V5_Common;
using System.IO;
using V5_Model;

namespace V5_EShop.Admin.DiyPage {
    public partial class Admin_DiyPage : System.Web.UI.Page {
        BLLDiyPage bllDiyPage = new BLLDiyPage();
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                string act = Easp.Req.GetString("action");
                Bind_ClassList();
                if (act == "edit") {
                    this.MultiViewList.ActiveViewIndex = 1;
                }
                else {
                    Bind_DataList();
                }
            }
        }
        private void Bind_ClassList() {
            BLLDiyPageClass bllDiyPageClass = new BLLDiyPageClass();
            DataTable dtClassList = bllDiyPageClass.GetDropDownList(0, "未归档");
            this.ddlClassList.DataSource = dtClassList.DefaultView;
            this.ddlClassList.DataTextField = "ClassName";
            this.ddlClassList.DataValueField = "ClassID";
            this.ddlClassList.DataBind();
        }

        private void Bind_DataList() {
            this.MultiViewList.ActiveViewIndex = 0;
            int RecordCount = 0;
            DataTable dt = bllDiyPage.GetList(Pager.PageSize, Pager.CurrentPageIndex, string.Empty, " ID Desc ", ref RecordCount).Tables[0];
            Pager.RecordCount = RecordCount;

            this.gvList.DataSource = dt.DefaultView;
            this.gvList.DataBind();
        }

        private void Bind_DataEdit(int id) {
            this.MultiViewList.ActiveViewIndex = 1;
            DataTable dt = bllDiyPage.GetList(" And ID=" + id).Tables[0];
            if (dt != null && dt.Rows.Count > 0) {
                DataView dv = dt.DefaultView;
                this.txtDiyName.Text = dv[0]["DiyName"].ToString();
                this.txtPagePath.Text = dv[0]["PagePath"].ToString();
                this.txtTemplate.Text = dv[0]["Template"].ToString();
                //this.ddlTemplate.SelectedIndex = this.ddlTemplate.Items.IndexOf(
                //    this.ddlTemplate.Items.FindByValue(dv[0]["Template"].ToString())
                //    );
                this.txtPageTitle.Text = dv[0]["PageTitle"].ToString();
                this.txtPageKeyWords.Text = dv[0]["PageKeyWords"].ToString();
                this.txtPageDescription.Text = dv[0]["PageDescription"].ToString();
                this.txtPageContent.Text = dv[0]["PageContent"].ToString();
                this.chkUserTemplate.Checked = dv[0]["IsTemplate"].ToString() == "1" ? true : false;

                this.txtPagePath.ReadOnly = true;

                this.hfID.Value = id.ToString();
                this.lblTitle.Text = "修改页面";
                this.btnSave.Text = "修改并保存";
            }
        }

        private void Bind_DataDel(int id) {
            try {
                object oFileName = new BLLSQL().Query("Select PagePath From S_DiyPage Where ID=" + id).Rows[0][0];
                string fileName = Easp.Str.SetString(oFileName);
                string pathFileName = Server.MapPath("~") + fileName;
                if (File.Exists(pathFileName)) {
                    File.Delete(pathFileName);
                }
                bllDiyPage.Delete(id);
            }
            catch (Exception ex) {
            }
            finally {
                Bind_DataList();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            int Id = Easp.Str.SetNumber(this.hfID.Value);
            int ParentID = 0;
            int ClassID = Easp.Str.SetNumber(this.ddlClassList.SelectedValue);
            string DiyName = this.txtDiyName.Text;
            string PagePath = this.txtPagePath.Text;
            string Template = this.txtTemplate.Text;// this.ddlTemplate.SelectedValue;
            string PageTitle = this.txtPageTitle.Text;
            string PageKeyWords = this.txtPageKeyWords.Text;
            string PageDescription = this.txtPageDescription.Text;
            string PageContent = this.txtPageContent.Text;
            int IsTemplate = Easp.Str.SetNumber(this.chkUserTemplate.Checked ? 1 : 0);
            ModelDiyPage model = new ModelDiyPage();
            model.ID = Id;
            model.ClassID = ClassID;
            model.ParentID = ParentID;
            model.DiyName = DiyName;
            model.PagePath = PagePath;
            model.IsTemplate = this.chkUserTemplate.Checked ? 1 : 0;
            model.Template = Template;
            model.PageTitle = PageTitle;
            model.PageKeyWords = PageKeyWords;
            model.PageDescription = PageDescription;
            model.PageContent = PageContent;
            if (Id == 0) {
                model.ID = bllDiyPage.GetMaxId();
                bllDiyPage.Add(model);
            }
            else if (Id > 0) {
                bllDiyPage.Update(model);
            }
            Bind_DataList();
        }

        protected void Pager_PageChanged(object sender, EventArgs e) {
            Bind_DataList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            string eName = e.CommandName;
            int id = Easp.Str.SetNumber(e.CommandArgument);
            switch (eName) {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_DataDel(id);
                    break;
            }
        }

        protected void btnExeSeleted_Click(object sender, EventArgs e) {
            int selected = Easp.Str.SetNumber(this.ddlOpID.SelectedValue);
            foreach (GridViewRow row in this.gvList.Rows) {
                CheckBox chk = (CheckBox)row.FindControl("chkItem");
                if (chk.Checked) {
                    int id = Easp.Str.SetNumber(row.Cells[1].Text);
                    if (selected == 1) {
                        object oFileName = new BLLSQL().Query("Select PagePath From S_DiyPage Where ID=" + id).Rows[0][0];
                        string fileName = Easp.Str.SetString(oFileName);
                        string pathFileName = Server.MapPath("~") + fileName;
                        if (File.Exists(pathFileName)) {
                            File.Delete(pathFileName);
                        }
                        bllDiyPage.Delete(id);
                    }
                }
            }
            this.Bind_DataList();
        }

        public ModelDiyPageClass BindClass(object objClassID) {
            int ClassID = Easp.Str.SetNumber(objClassID);
            ModelDiyPageClass m = new BLLDiyPageClass().GetModel(ClassID);
            if (m == null) {
                m = new ModelDiyPageClass();
            }
            return m;
        }
    }
}